package com.alibaba.citrus.ots.unified.inventory.repository.impl;

import com.alibaba.citrus.ots.unified.inventory.repository.AllocateRelationWriteRepository;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.dto.CreateAllocateRelationResponse;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.dto.DeleteByIdsRequest;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.dto.DeleteByIdsResponse;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.dto.UpdateAllocateRelationResponse;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.model.AllocateRelation;
import com.epoch.app.otsunifiedinventory.domain.allocaterelation.service.AllocateRelationService;
import com.epoch.app.otsunifiedinventory.sdo.AllocateRelationSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 王成
 * @date:
 **/
@Component
public class AllocateRelationWriteRepositoryImpl implements AllocateRelationWriteRepository {

    @Resource
    AllocateRelationService allocateRelationService;


    @Override
    public String save(AllocateRelationSDO allocateRelationSDO)  {
        AllocateRelation allocateRelation = new AllocateRelation();
        allocateRelation.setCustomerCode(allocateRelationSDO.getCustomerCode());
        allocateRelation.setCustomerName(allocateRelationSDO.getCustomerName());
        allocateRelation.setOrganizationCode(allocateRelationSDO.getOrganizationCode());
        allocateRelation.setOrganizationName(allocateRelationSDO.getOrganizationName());
        allocateRelation.setOrganizationOutCode(allocateRelationSDO.getOrganizationOutCode());
        allocateRelation.setSaleChannelCode(allocateRelationSDO.getSaleChannelCode());
        allocateRelation.setSaleChannelName(allocateRelationSDO.getSaleChannelName());
        allocateRelation.setSaleChannelOutCode(allocateRelationSDO.getSaleChannelOutCode());
        allocateRelation.setDepartmentCode(allocateRelationSDO.getDepartmentCode());
        allocateRelation.setDepartmentName(allocateRelationSDO.getDepartmentName());
        allocateRelation.setDepartmentOutCode(allocateRelationSDO.getDepartmentOutCode());
        allocateRelation.setWareHouseCode(allocateRelationSDO.getWareHouseCode());
        allocateRelation.setWareHouseName(allocateRelationSDO.getWareHouseName());
        allocateRelation.setWareHouseOutCode(allocateRelationSDO.getWareHouseOutCode());
        allocateRelation.setReceiveWareHouseCode(allocateRelationSDO.getReceiveWareHouseCode());
        allocateRelation.setReceiveWareHouseName(allocateRelationSDO.getReceiveWareHouseName());
        allocateRelation.setRemark(allocateRelationSDO.getRemark());
//        allocateRelation.setGmtCreate(new Date());

        allocateRelation.setIsEnable(allocateRelationSDO.getIsEnable());
        allocateRelation.setIsCheckInventory(allocateRelationSDO.getIsCheckInventory());
        allocateRelation.setIsStrategyFirst(allocateRelationSDO.getIsStrategyFirst());

        //add by myw,add address info of allocate relation function
        allocateRelation.setAddressCode(allocateRelationSDO.getAddressCode());
        allocateRelation.setAddressId(allocateRelationSDO.getAddressId());
        allocateRelation.setAddressName(allocateRelationSDO.getAddressName());
        CreateAllocateRelationResponse allocateRelationResponse = allocateRelationService.createAllocateRelation(allocateRelation);
        String lastInsertId = allocateRelationResponse.getLastInsertId();
        return lastInsertId;
        /*//判断是否有重复
        AllocateRelationLoadListRequest allocateRelationLoadListRequest = new AllocateRelationLoadListRequest();
        if (StringUtils.isNotEmpty(allocateRelation.getSaleChannelCode())){
            allocateRelationLoadListRequest.setSaleChannelCode(allocateRelation.getSaleChannelCode());
        }
        if (StringUtils.isNotEmpty(allocateRelation.getDepartmentCode())){
            allocateRelationLoadListRequest.setDepartmentCode(allocateRelation.getDepartmentCode());
        }
        if (StringUtils.isNotEmpty(allocateRelation.getCustomerCode())){
            allocateRelationLoadListRequest.setCustomerCode(allocateRelation.getCustomerCode());
        }
        Result<List<AllocateRelation>> listResult = allocateRelationService.loadAllocateRelationList(allocateRelationLoadListRequest);
        if(CollectionUtils.isNotEmpty(listResult.getResult())){
//            throw new Exception("渠道、部门、客户已经创建");TODO 渠道部门客户相同不创建
            throw new FunctionException("OTS-04-001-00-15-011");
//            return null;
        }else{
            CreateAllocateRelationResponse allocateRelationResponse = allocateRelationService.createAllocateRelation(allocateRelation);
            String lastInsertId = allocateRelationResponse.getLastInsertId();
            return lastInsertId;
        }*/
    }

    @Override
    public void modify(AllocateRelationSDO allocateRelationSDO) {
        AllocateRelation allocateRelation = new AllocateRelation();
        if (null!=allocateRelationSDO.getId()){
            allocateRelation.setId(Long.parseLong(allocateRelationSDO.getId()));
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getCustomerCode())){
            allocateRelation.setCustomerCode(allocateRelationSDO.getCustomerCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getCustomerName())){
            allocateRelation.setCustomerName(allocateRelationSDO.getCustomerName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getOrganizationCode())){
            allocateRelation.setOrganizationCode(allocateRelationSDO.getOrganizationCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getOrganizationName())){
            allocateRelation.setOrganizationName(allocateRelationSDO.getOrganizationName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getSaleChannelCode())){
            allocateRelation.setSaleChannelCode(allocateRelationSDO.getSaleChannelCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getSaleChannelName())){
            allocateRelation.setSaleChannelName(allocateRelationSDO.getSaleChannelName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getDepartmentCode())){
            allocateRelation.setDepartmentCode(allocateRelationSDO.getDepartmentCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getDepartmentName())){
            allocateRelation.setDepartmentName(allocateRelationSDO.getDepartmentName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getWareHouseCode())){
            allocateRelation.setWareHouseCode(allocateRelationSDO.getWareHouseCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getWareHouseName())){
            allocateRelation.setWareHouseName(allocateRelationSDO.getWareHouseName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getReceiveWareHouseCode())){
            allocateRelation.setReceiveWareHouseCode(allocateRelationSDO.getReceiveWareHouseCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getReceiveWareHouseName())){
            allocateRelation.setReceiveWareHouseName(allocateRelationSDO.getReceiveWareHouseName());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getRemark())){
            allocateRelation.setRemark(allocateRelationSDO.getRemark());
        }
        if (null!=allocateRelationSDO.getIsEnable()){
            allocateRelation.setIsEnable(allocateRelationSDO.getIsEnable());
        }
        if (null!=allocateRelationSDO.getIsCheckInventory()){
            allocateRelation.setIsCheckInventory(allocateRelationSDO.getIsCheckInventory());
        }
        if (null!=allocateRelationSDO.getIsStrategyFirst()){
            allocateRelation.setIsStrategyFirst(allocateRelationSDO.getIsStrategyFirst());
        }
        if (null!=allocateRelationSDO.getAddressName()){
            allocateRelation.setAddressName(allocateRelationSDO.getAddressName());
        }
        if (null!=allocateRelationSDO.getAddressCode()){
            allocateRelation.setAddressCode(allocateRelationSDO.getAddressCode());
        }
        if (null!=allocateRelationSDO.getAddressId()){
            allocateRelation.setAddressId(allocateRelationSDO.getAddressId());
        }

//        allocateRelation.setGmtModified(new Date());
        UpdateAllocateRelationResponse updateAllocateRelationResponse = allocateRelationService.updateAllocateRelation(allocateRelation);
    }

    @Override
    public Integer deleteByIds(List<String> ids) {
        DeleteByIdsRequest deleteByIdsRequest = new DeleteByIdsRequest();
        deleteByIdsRequest.setIds(ids);
        DeleteByIdsResponse deleteByIdsResponse = allocateRelationService.deleteByIds(deleteByIdsRequest);
        Integer count = deleteByIdsResponse.getCount();
        return count;
    }
}
